package com.lry.basic.spring.ioc;

import java.util.ArrayList;
import java.util.List;

public class PreBuildBeanFactory extends DefaultBeanFactory{


    private List<String> beanNames = new ArrayList<>();

    @Override
    public void registerBeanDefinition(String beanName,BeanDefinition beanDefinition){
        super.registerBeanDefinition(beanName,beanDefinition);
        synchronized (beanNames){
            beanNames.add(beanName);
        }
    }
    //提前实例化非懒加载的单例bean
    public void preInstantiateSingletons() throws Exception{
        synchronized (beanNames){
            for (String name : beanNames){
                BeanDefinition beanDefinition = this.getBeanDefinition(name);
                if (beanDefinition.isSingleton()&&!beanDefinition.isLazyInit()){
                    this.doGetBean(name);
                }
            }
        }
    }
}
